#include "io430x22x4.h"
#include "intrinsics.h"
#include "sem_logger.h"
#include "sem_core.h"



//---------------  Variables  ----------------

static int head;
static int tail;
static log_item_t log_items[MAX_LOG_ITEMS];
 
static int registered_id;
static int registered_sequence;


///----- interno
void log_item_add(log_item_t in){
  log_items[head] = in;
  head++;
  if (head == MAX_LOG_ITEMS){
    head = 0;
  }
}

log_item_t log_item_get(){
  log_item_t item_to_return;
  item_to_return = log_items[tail];
  tail++;
  if (tail == MAX_LOG_ITEMS){
     tail = 0;
  }
  
  return item_to_return;
}

int log_items_isempty(){
  if  (tail == head ){
    return 1; // true, queue is empty
  } else {
    return 0; // false, queue is not empty
  }
}

int log_items_isfull(){
  if ( (head+1 == tail) || ((head+1 == MAX_LOG_ITEMS ) && (tail == 0) ) ){
    return 1; // true, queue is full
  } else {
    return 0; // false, queue is not full
  }
}

void log_q_with_data(float q){
  log_item_t new_log_item;
  new_log_item.q = q;
  new_log_item.id= registered_id;
  new_log_item.sequence= registered_sequence;
 
  log_item_add(new_log_item);
}

///----- publico
void sem_log_init(){
  // reset position index
  head = 0; 
  tail = 0;
}

void sem_log_partial_q(int id, int sequence){
  registered_id = id;
  registered_sequence = sequence;
  sem_get_partial_metering(log_q_with_data);
}


